﻿using Juli.Mes.Ladles.Dtos;
using Lion.AbpPro.Core;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;

namespace Juli.Mes.Ladles
{
    [Route("Ladles")]
    [Authorize]
    public class LadleAppService : MesAppService, ILadleAppService
    {
        private readonly IRepository<Ladle> ladleRepository;
        private readonly IRepository<IdentityUser> userRepository;

        public LadleAppService(IRepository<Ladle> ladleRepository,
             IRepository<IdentityUser> userRepository)
        {
            this.ladleRepository = ladleRepository;
            this.userRepository= userRepository;
        }

        [HttpPost("create")]
        [SwaggerOperation("新增钢水罐", Tags = new[] { "Ladles" })]
        public async Task CreateLadle(LadleInputDto input)
        {
            var entity = ObjectMapper.Map<LadleInputDto, Ladle>(input);
            await ladleRepository.InsertAsync(entity);
        }

        [HttpPost("delete")]
        [SwaggerOperation("删除钢水罐", Tags = new[] { "Ladles" })]
        public async Task DeleteLadle(IdInput input)
        {
            await ladleRepository.DeleteAsync(p => p.Id == input.Id);
        }

        [HttpPost("edit")]
        [SwaggerOperation("编辑钢水罐", Tags = new[] { "Ladles" })]
        public async Task EditLadle(LadleInputDto input)
        {
            var entity = await ladleRepository.GetAsync(p => p.Id == input.Id);
            ObjectMapper.Map(input, entity);
            await ladleRepository.UpdateAsync(entity);
        }

        [HttpPost("page")]
        [SwaggerOperation("分页查询钢水罐", Tags = new[] { "Ladles" })]
        public async Task<PagedResultDto<LadleOutputDto>> ListLadle(PageLadleInputDto input)
        {
            var stoveQuery = await ladleRepository.GetQueryableAsync();
            var userQuery = await userRepository.GetQueryableAsync();

            var query = from s in stoveQuery
                        join u1 in userQuery on s.CreatorId equals u1.Id
                        join u2 in userQuery on s.LastModifierId equals u2.Id into u22
                        from u23 in u22.DefaultIfEmpty()
                        select new LadleOutputDto()
                        {
                            Id = s.Id,
                            LadleCode = s.LadleCode,
                            LadleName = s.LadleName,
                            Description = s.Description,
                            Rfid = s.Rfid,
                            CreationTime = s.CreationTime,
                            LastModificationTime = s.LastModificationTime,
                            CreatorName = u1.Name,
                            LastModifierName = u23.Name,
                            SkinWeight = s.SkinWeight,
                            Material =s.Material,
                            Active = s.Active
                        };

            query = query.WhereIf(!string.IsNullOrEmpty(input.Filter),
                p => p.LadleCode.Contains(input.Filter)
                || p.LadleName.Contains(input.Filter)
                || p.Rfid.Contains(input.Filter));

            var items = query
                .OrderBy(e => e.CreationTime)
                .PageBy(input.SkipCount, input.PageSize)
                .ToList();

            var count = query.Count();

            return new PagedResultDto<LadleOutputDto>()
            {
                Items = items,
                TotalCount = count,
            };
        }
    }
}
